package team.hyznrj.studentsys.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.hyznrj.studentsys.dao.repository.OrganizeMarkRepository;
import team.hyznrj.studentsys.dao.repository.ScienceMarkRepository;
import team.hyznrj.studentsys.dao.repository.SpecialMarkRepository;
import team.hyznrj.studentsys.entity.OrganizeMark;
import team.hyznrj.studentsys.entity.ScienceMark;
import team.hyznrj.studentsys.entity.SpecialMark;
import team.hyznrj.studentsys.enums.MarkEnum;
import team.hyznrj.studentsys.exception.MarkException;
import team.hyznrj.studentsys.service.SimpleMarkService;

import java.util.List;

/**
 * @author ReMidDream
 * @date 2018-04-11 16:01
 **/
@Service
public class SimpleMarkServiceImpl implements SimpleMarkService {

    @Autowired
    private ScienceMarkRepository scienceMarkRepository;
    @Autowired
    private OrganizeMarkRepository organizeMarkRepository;
    @Autowired
    private SpecialMarkRepository specialMarkRepository;

    @Override
    public List<ScienceMark> findScienceMarkByIds(List<String> ids) {
        return scienceMarkRepository.findAll(ids);
    }

    @Override
    @Transactional
    public boolean updateScienceMarks(List<ScienceMark> scienceMarks) {
        for (ScienceMark scienceMark : scienceMarks) {
            if (scienceMark.getConductScoreId()==null) {
                throw new MarkException(MarkEnum.PARAMRMETER_ERROR);
            }
        }
        List<ScienceMark> save = scienceMarkRepository.save(scienceMarks);
        return true;
    }

    @Override
    public List<OrganizeMark> findOrganizeMarkByIds(List<String> ids) {
        return organizeMarkRepository.findAll(ids);
    }

    @Override
    public boolean updateOrganizeMarks(List<OrganizeMark> organizeMarks) {
        for (OrganizeMark organizeMark : organizeMarks) {
            if (organizeMark.getConductScoreId()==null) {
                throw new MarkException(MarkEnum.PARAMRMETER_ERROR);
            }
        }
        List<OrganizeMark> save = organizeMarkRepository.save(organizeMarks);
        return true;
    }

    @Override
    public List<SpecialMark> findSpecialMarkByIds(List<String> ids) {
        return specialMarkRepository.findAll(ids);
    }

    @Override
    public boolean updateSpecialMarks(List<SpecialMark> specialMarks) {
        for (SpecialMark specialMark : specialMarks) {
            if (specialMark.getConductScoreId()==null) {
                throw new MarkException(MarkEnum.PARAMRMETER_ERROR);
            }
        }
        List<SpecialMark> save = specialMarkRepository.save(specialMarks);
        return true;
    }
}
